//
//  CollectViewController.m
//  TravelDiary
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 YDB MAC. All rights reserved.
//

#import "CollectViewController.h"
#import "CollectCell.h"
#import "Collect.h"
#import <AVOSCloud.h>

@interface CollectViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *dataArrayLD;

@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadTableView];
    [self loadData];
    
    
}
- (void)loadTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CollectCell" bundle:nil] forCellReuseIdentifier:@"CollectCell"];
}
- (void)loadData{
    AVQuery *query = [AVQuery queryWithClassName:@"Collect"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        _dataArray = [NSMutableArray array];
        for (NSDictionary * dic in objects) {
            if ([dic[@"userName"] isEqualToString:[AVUser currentUser].username]) {
                Collect *modle = [[Collect alloc] init];
                
                [modle setValuesForKeysWithDictionary:dic[@"localData"]];
                
                modle.creatDate = [NSString stringWithFormat:@"%@",dic[@"createdAt"]];
                
                [_dataArray addObject: modle];
                [self.tableView reloadData];
            }
            
            
            
            
        }
    }];
}
#pragma mark --tableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"CollectCell";
    CollectCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    Collect *model = self.dataArray[indexPath.row];
    
    cell.titleLabel.text = model.title;
    
    
    NSString *dateStr = [model.creatDate substringToIndex:20];
    cell.dateLabel.text = [NSString stringWithFormat:@"收藏时间:%@",dateStr];
    
    cell.selectedBackgroundView = [[UIView alloc]init];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了Cell");
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
